package com.example.volunteeroa.controller;

import com.example.volunteeroa.JpaUtils.DeleteUtils;
import com.example.volunteeroa.entity.*;
import com.example.volunteeroa.repository.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@CrossOrigin//跨域
@RestController
@RequestMapping("api/organization")
public class OrganizationController {
    @Resource
    VenueRepository VERepository;
    @Resource
    OrganizationRepository ORepository;
    @Resource
    VolunteerRepository VRepository;
    @Resource
    OrganizationAllocationRepository OARepository;
    @Resource
    DivisionRepository DRepository;

    @PostMapping("/findOrgByID")
    public RestBean findOrgAccordingVenueID(@RequestBody Organization organization){
        Organization organization1 = ORepository.findById(organization.getOrgID()).get();
        return new RestBean(200,"查询成功",organization1);
    }//通过venueID查找venue下对应的所有组织

    @GetMapping()
    public RestBean findAll(){
        List<Organization> all = ORepository.findAll();
        return new RestBean(200,"查询全部成功",all);
    }//查询全部

    @DeleteMapping()
    public RestBean deleteOrgByID(@RequestBody Organization organization){
        DeleteUtils.deleteOrgAllocByOrgID(ORepository,OARepository,organization);
        DeleteUtils.deleteOrganization(VERepository,ORepository,VRepository,organization);
        return new RestBean(200,"删除成功",null);
    }//通过组织名字删除组织信息

    @PutMapping()
    public RestBean updateVolunteerNum(@RequestBody Organization organization){
        Organization organization1 = ORepository.findById(organization.getOrgID()).get();
        Division division = DRepository.findById(organization1.getDivision().getDid()).get();
        Venue venue = VERepository.findById(organization1.getVenue().getVenueID()).get();
        organization.setDivision(division);
        organization.setVenue(venue);
        ORepository.save(organization);
        return new RestBean(200,"修改成功",null);
    }//修改组织下的志愿者数量
    @PutMapping("/{venueID}")
    public RestBean update(@RequestBody Organization organization,@PathVariable Integer venueID){
        Organization organization1 = ORepository.findById(organization.getOrgID()).get();
        Division division = DRepository.findById(organization1.getDivision().getDid()).get();
        Venue venue = VERepository.findById(venueID).get();
        organization.setDivision(division);
        organization.setVenue(venue);
        ORepository.save(organization);
        return new RestBean(200,"修改成功",null);
    }//修改组织的信息

    @PostMapping("/{did}/{venueID}")
    public RestBean add(@RequestBody Organization organization,@PathVariable Integer venueID,@PathVariable Integer did){
        Division division = DRepository.findById(did).get();
        Venue venue = VERepository.findById(venueID).get();
        organization.setDivision(division);
        organization.setVenue(venue);
        ORepository.save(organization);
        return new RestBean(200,"添加成功",null);
    }//添加一个新的组织
}
